package com.woniuxy.controller;


import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.TbDepartment;
import com.woniuxy.entitys.TbWorker;
import com.woniuxy.entitys.TbHospital;
import com.woniuxy.service.DepartmentService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginUser;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class DepartmentController {
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RedisTemplate redisTemplate;
    //查询所有的医院(包含科室);
    @GetMapping("/getHospitals")
    public ResultVO getHospitals(){
        List<TbHospital> list =  departmentService.getHospitals();
        return list!=null?ResultVO.success("查询医院成功",list):ResultVO.failure("查询医院失败");
    }

    @GetMapping("/getDepartments")
    public ResultVO getDepartment(){
        List<TbDepartment> list =  departmentService.getDepartments();
        return list!=null?ResultVO.success("查询科室成功",list):ResultVO.failure("查询科室失败");
    }

    //2.查询所有的科室根据医院ID;
    @PostMapping("/getDepartmentByHid")
    public ResultVO getDepartmentByHid(@RequestBody Map<String,Object> map){
        List<Integer> hids = (ArrayList<Integer>) map.get("hid");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Integer hid = hids.get(0);


        PageInfo<TbDepartment> list =  departmentService.getDepartmentByHid(hid,pageNum,pageSize);
        return list!=null?ResultVO.success("查询科室成功",list):ResultVO.failure("查询科室失败");
    }
    @GetMapping("/department/getAllDepartment")
    public ResultVO getAllDepartment(){
        List<TbDepartment> allDepartment = departmentService.getAllDepartment();
        return allDepartment!=null?ResultVO.success("科室查询成功",allDepartment):ResultVO.failure("科室查询失败");
    }

    @GetMapping("/departmentList")
    public ResultVO getDepartmentList(){
        List<TbDepartment> allDepartment = departmentService.getAllDepartment();
        return allDepartment!=null?ResultVO.success("科室查询成功",allDepartment):ResultVO.failure("科室查询失败");
    }

    //查询登陆人管辖的科室
    @GetMapping("/department/departmentList")
    public ResultVO getDepartments(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String header = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(header);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:" + username);
        Integer uid = loginUser.getUser().getUid();
        List<TbDepartment> departmentList = departmentService.selDepartmentsByUid(uid);
        return departmentList!=null?ResultVO.success("success",departmentList):ResultVO.failure("error");
    }
    @PostMapping("/hospital/delDepartment/{did}")
    public ResultVO softdelDepartment(@PathVariable("did") Integer did){
        int i = departmentService.softdelDep(did);
        return i>0?ResultVO.success("ok"):ResultVO.failure("error");
    }

    @PostMapping("/hospital/condition")
    public ResultVO getWorkerByCondition(@RequestBody Map<String,Object> map){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String header = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(header);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:" + username);
        Integer uid = loginUser.getUser().getUid();
        String keyword = (String) map.get("keyword");
        List<TbDepartment> departmentList = departmentService.selByCondition(uid,keyword);
        return departmentList!=null?ResultVO.success("success",departmentList):ResultVO.failure("error");
    }
    @PostMapping("/hospital/saveAdd")
    public ResultVO saveAddDepartment(@RequestBody Map<String,Object> map){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String header = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(header);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:" + username);
        Integer uid = loginUser.getUser().getUid();
        String dname = (String) map.get("dname");
        System.out.println(map+"xxxxxxxxxxxxxxxxxx");
        int i = departmentService.saveAdd(dname,uid);
        return i>0?ResultVO.success("success"):ResultVO.failure("error");

    }
}
